#this script creates some objects that are used to create some of the graph

library(foreign)	
load("n.sims.image") #get n.sims from Script 1, which needs to be run 1st

#read in data--at nominee level
nomd <- read.dta("simulated_nom_level_data_from_r_new.dta", convert.underscore=T)
head(nomd)
nomd <- nomd[order(nomd$nominee, nomd$simulation),]

bailey.nomd <-subset(nomd, bailey.observation==1)

nom.level.stats <- data.frame(nominee=unique(nomd$nominee))
nom.level.stats$date.vote <- tapply(nomd$date.vote, nomd$nominee,unique)
nom.level.stats$confirmed <- tapply(nomd$confirmed, nomd$nominee,unique)
nom.level.stats$rcvote <- tapply(nomd$rcvote, nomd$nominee,unique)
nom.level.stats$bailey.observation <- tapply(nomd$bailey.observation, nomd$nominee,unique)
bailey.nom.level.stats <- subset(nom.level.stats, bailey.observation==1)

#create title case for graphs
	#do separtely for DW and biley
temp1 <- substring(nomd$nominee,1,1)
temp2 <- tolower(substring(nomd$nominee,2))
nominee.title.dw <- paste(temp1,temp2, sep="")
nominee.title.dw <- ifelse(nominee.title.dw=="Rehnquist2", "Rehnquist (CJ)",
                        ifelse(nominee.title.dw=="Fortas2", "Fortas (CJ)", 
												ifelse(nominee.title.dw=="Stone2", "Stone (CJ)",
													nominee.title.dw)))
													
temp <- toupper(substring(nominee.title.dw,1,1))
temp1 <- substring(nominee.title.dw,2, nchar(nominee.title.dw))
nominee.title.dw <- paste(temp, temp1,sep="")                   

nominee.title.dw <- ifelse(nominee.title.dw=="Oconnor", "O'Connor",
								 ifelse(nominee.title.dw=="Ginsburg_d", "D. Ginsburg",
								 ifelse(nominee.title.dw=="Ginsburg_r", "R.B. Ginsburg",
								 ifelse(nominee.title.dw=="Roberts_j", "Roberts", 
								 ifelse(nominee.title.dw=="Rehnquist1", "Rehnquist (AJ)",
								 ifelse(nominee.title.dw=="Rehnquist2", "Rehnquist (CJ)",
							 	ifelse(nominee.title.dw=="Fortas1", "Fortas (AJ)", 
                 ifelse(nominee.title.dw=="Fortas2", "Fortas (CJ)", 
							   ifelse(nominee.title.dw=="Stone2", "Stone (CJ)",
							 	 nominee.title.dw)))))))))
						
					
nomd$nominee.title.dw <- nominee.title.dw 
nom.level.stats$nominee.title.dw <- unique(nominee.title.dw)
nominee.title.dw <- unique(nominee.title.dw)

#Bailey
temp1 <- substring(bailey.nomd$nominee,1,1)
temp2 <- tolower(substring(bailey.nomd$nominee,2))
nominee.title.bly <- paste(temp1,temp2, sep="")
nominee.title.bly <- ifelse(nominee.title.bly=="Rehnquist2", "Rehnquist (CJ)",
                        ifelse(nominee.title.bly=="Fortas2", "Fortas (CJ)", nominee.title.bly))
temp <- toupper(substring(nominee.title.bly,1,1))
temp1 <- substring(nominee.title.bly,2, nchar(nominee.title.bly))
nominee.title.bly <- paste(temp, temp1,sep="")                   

nominee.title.bly <- ifelse(nominee.title.bly=="Oconnor", "O'Connor",
								 ifelse(nominee.title.bly=="Ginsburg_d", "D. Ginsburg",
								 ifelse(nominee.title.bly=="Ginsburg_r", "R.B. Ginsburg",
								 ifelse(nominee.title.bly=="Roberts_j", "Roberts", 
								 ifelse(nominee.title.bly=="Rehnquist1", "Rehnquist (AJ)",
								 ifelse(nominee.title.bly=="Rehnquist2", "Rehnquist (CJ)",
							 	ifelse(nominee.title.bly=="Fortas1", "Fortas (AJ)", 
                 ifelse(nominee.title.bly=="Fortas2", "Fortas (CJ)", 
							   ifelse(nominee.title.bly=="Stone2", "Stone (CJ)",
							 	 nominee.title.bly)))))))))
						

bailey.nomd$nominee.title.bly <- nominee.title.bly
bailey.nom.level.stats$nominee.title.bly  <- unique(nominee.title.bly )
nominee.title.bly  <- unique(nominee.title.bly )